package notwork;

import java.io.IOException;
import java.net.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class UdpEchoClient_2 {

    private DatagramSocket socket=null;
    private int Serverport;
    private String ServerIp;

    private UdpEchoClient_2(String serverIp, int serverport) throws SocketException {
        Serverport = serverport;
        ServerIp = serverIp;
        socket = new DatagramSocket();
    }

    public void start() throws IOException {
        while(true){
            System.out.println("请输入请求内容：");
            Scanner scanner=new Scanner(System.in);
            String request=scanner.next();
            DatagramPacket requestpacket=new DatagramPacket(request.getBytes(),request.getBytes().length,
                    InetAddress.getByName(ServerIp),Serverport);
            socket.send(requestpacket);


            DatagramPacket reponsePacket=new DatagramPacket(new byte[4096],4096);
                    socket.receive(reponsePacket);
                    String reponse=new String(reponsePacket.getData(),0,reponsePacket.getLength());
            System.out.println(reponse);
        }
    }

    public static void main(String[] args) throws IOException {
        UdpEchoClient_2 client_2=new UdpEchoClient_2("127.0.0.1",8989);
        client_2.start();
    }
}
